package cn.gs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


import cn.gs.domain.Customer;
import cn.gs.domain.LinkMan;
import cn.gs.utils.HibernateUtil;


@Component(value="linkManDao")
@Scope("prototype")
public class LinkManDaoImpl implements LinkManDao {

	
	
	Session session = HibernateUtil.openSession();
	Transaction transaction = session.getTransaction();
	
	public void add(LinkMan l) {
		transaction.begin();
		session.save(l);
		transaction.commit();
		session.close();
		
	}

	
	public void delete(Long id) {
		transaction.begin();
		LinkMan l = (LinkMan) session.get(LinkMan.class, id);
		session.delete(l);
		transaction.commit();
		session.close();		
	}

	
	public LinkMan selectByID(Long id) {
		transaction.begin();	
		LinkMan l = (LinkMan) session.get(LinkMan.class, id);
		transaction.commit();
		session.close();
		return l;
	}

	
	public void update(LinkMan c) {
		transaction.begin();
		System.out.println(c.toString());
		session.saveOrUpdate(c);		
		transaction.commit();
		session.close();
		
	}

	
	public List<LinkMan> selName(String name) {
		transaction.begin();
		System.out.println("核心+lsit1"+name);
		Query query=session.createQuery("from LinkMan where lkm_name like ?");
		query.setParameter(0, "%"+name+"%");
		List list=query.list();	
		System.out.println("核心+lsit"+list);
		transaction.commit();
		session.close();
		return list;	
	}

	
	public List<LinkMan> selectAll() {
		transaction.begin();
		List list=session.createQuery("from linkMan").list();
		transaction.commit();
		session.close();
		return list;
	}

	
	public List<LinkMan> selectPageCount() {
		transaction.begin();
		Query query = session.createQuery("select count(l) from LinkMan l");
		List list = query.list();
		transaction.commit();
		session.close();
		return list;
	}

	
	public List<LinkMan> paging(int page) {
		transaction.begin();	
		Query query = session.createQuery("from LinkMan");
		int i=(page-1)*3;
		query.setFirstResult(i);
		query.setMaxResults(3);
		List<LinkMan> list = query.list();
		transaction.commit();
		session.close();
		return list;
	}
	
	
	public List<Customer> selectCustom() {
		transaction.begin();
		Query query =session.createQuery("from Customer");
		List<Customer> list=query.list();
		Customer c=list.get(0);
		transaction.commit();
		session.close();
		return list;		
	}
}
